package firstping.controller;

import java.io.IOException;
import java.util.List;
import java.util.StringTokenizer;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import firstping.data.QnAPersistenceManager;
import firstping.model.SaveDomain;
import firstping.string.Utils;

@SuppressWarnings("serial")
public class SearchServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html");
		//resp.getWriter().println("Hello, world");
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		
		String title_url = Utils.fomatDomain(req.getParameter("name"));
		System.out.println(title_url);
		if(Utils.checkDomain(title_url))
		{
			new QuickCheck("http://"+title_url);
			
			Query query = psm.newQuery(SaveDomain.class);
			query.setFilter("name=='"+"http://"+title_url+"'");
			query.setOrdering("longDate desc");
			query.setRange(0,15);
			@SuppressWarnings("unchecked")
			List<SaveDomain> list = (List<SaveDomain>)query.execute();
			
			req.setAttribute("list", list);
			req.setAttribute("site", "http://"+title_url);
			
			req.setAttribute("title", "Statistics status for : "+"http://"+title_url);
			req.setAttribute("keyword", "account,monitoring domain,firstping,statistics site,"+title_url);
			req.setAttribute("description", "Statistics status for : "+"http://"+title_url+".We provide web site monitoring to webmasters. Monitor your web site is availability 24 hours a day, 7 days a week with instant email alerts and weekly web site performance statistics.");
			
			try {
				req.getRequestDispatcher("/view.jsp").forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			resp.getWriter().println("<a href='/'>Home page</a><br/><br/>Domain does not exist ! <br/><br/><a href='javascript:void(0)' onclick='history.go(-1)'>Back</a>");
		}
	}

}
